package com.yeebo.domain.config;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yeebo.dao.config.GlobalParamDao;
import com.yeebo.model.config.GlobalParam;
import com.yeebo.utils.DateUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component("globalParamDomain")
public class GlobalParamDomain {

    @Resource
    private GlobalParamDao globalParamDao;

    public GlobalParam findByParamName(String paramName) {
        return globalParamDao.findByParamName(paramName);
    }

    public Integer getIntegerVal(String paramName) {
        GlobalParam globalParam = findByParamName(paramName);
        if (globalParam == null) {
            return null;
        }
        return Integer.parseInt(globalParam.getParamValue());
    }

    public String getStringVal(String paramName) {
        GlobalParam globalParam = findByParamName(paramName);
        if (globalParam == null) {
            return null;
        }
        return globalParam.getParamValue();
    }

    public Date getDateVal(String paramName) {
        GlobalParam globalParam = findByParamName(paramName);
        if (globalParam != null) {
            return DateUtils.stringToDate(globalParam.getParamValue(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        }
        return null;
    }

    public void addGlobalParam(GlobalParam addEntity) {
        globalParamDao.add(addEntity);
    }

    public void updateGlobalParam(GlobalParam updateEntity) {
        globalParamDao.update(updateEntity);
    }

    public Page<GlobalParam> loadGlobalParamList(String paramDesc, Integer pageNo, Integer pageSize) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("paramDesc", paramDesc);

        PageHelper.startPage(pageNo, pageSize);
        Page<GlobalParam> resultPage = globalParamDao.loadGlobalParamList(queryMap);
        return resultPage;
    }

}
